package com.boredream.bdcodehelper.view;


import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.boredream.bdcodehelper.R;

@SuppressLint("ClickableViewAccessibility")
public class PositionBar extends LinearLayout {

    private String[] mItems = {"A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
            "V", "W", "X", "Y", "Z"};

    private OnPositionChangedListener listener;

    public void setOnPositionChangedListener(OnPositionChangedListener listener) {
        this.listener = listener;
    }

    public PositionBar(Context context) {
        super(context);
        init();
    }

    public PositionBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PositionBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setOrientation(VERTICAL);
        setGravity(Gravity.CENTER_HORIZONTAL);
        setItems(mItems);
    }

    public void setItems(String[] items) {
        mItems = items;
        removeAllViews();
        for (CharSequence s : mItems) {
            TextView t = new TextView(getContext());
            t.setText(s);
            t.setTextSize(14);
            LayoutParams params = new LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, 0);
            params.weight = 1;
            t.setLayoutParams(params);
            t.setTextColor(getResources().getColor(R.color.txt_black));
            addView(t);
        }
    }

    public String[] getItems() {
        return mItems;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        final int action = ev.getAction();
        TextView child;

        switch (action & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                child = findChildByLocation(ev.getX(), ev.getY());
                if (listener != null) {
                    listener.onPositionSelected(child == null ? "" : child.getText().toString());
                }
                break;
            case MotionEvent.ACTION_MOVE:
                child = findChildByLocation(ev.getX(), ev.getY());
                if (listener != null) {
                    listener.onPositionSelected(child == null ? "" : child.getText().toString());
                }
                break;
            case MotionEvent.ACTION_UP:
                if (listener != null) {
                    listener.onPositionSelected("");
                }
                break;
        }

        return true;
    }

    private TextView findChildByLocation(float x, float y) {
        TextView child = null;
        int mContentTop = getChildAt(0).getTop();
        int mContentBottom = getChildAt(getChildCount() - 1).getBottom();
        int defSize = (mContentBottom - mContentTop) / mItems.length;

        int index = (int) ((y - mContentTop) / defSize);
        if (index >= 0 && index < mItems.length
                && x >= 0 && x <= getWidth()) {
            child = (TextView) getChildAt(index);
        }
        return child;
    }

    public interface OnPositionChangedListener {
        void onPositionSelected(String key);
    }

}